import { findCartList, insertCart, deleteCart } from '@/api/cart'

// 购物车状态
export default {
  namespaced: true,
  state: () => {
    return {
      list: [] // 购物车列表
    }
  },
  mutations: {
    // 获取购物车数据
    setList (state, cartList) {
      state.list = cartList
    }

  },
  actions: {
    // 加入购物车
    async asyncInsertCart (ctx, good) {
      // console.log(good)
      // 调用添加购物车接口
      // 拉取购物车列表存入vuex
      // 发送接口
      await insertCart({ skuId: good.skuId, count: good.count })

      // 拉取最新的购物车列表
      const res = await findCartList()

      return new Promise((resolve, reject) => {
        ctx.commit('setList', res.result)
        resolve()
      })
    },

    // 删除购物车商品
    async asyncDelCart (ctx, good) {
      const skuId = good.skuId
      await deleteCart([skuId])
      // 获取最新购物车
      const res = await findCartList()

      return new Promise((resolve, reject) => {
        ctx.commit('setList', res.result)
        resolve()
      })
    },

    // 异步更新购物车
    async asyncUpdateList (ctx) {
      // 拉取最新的购物车列表
      // 拉取最新的购物车列表
      const res = await findCartList()

      return new Promise((resolve, reject) => {
        ctx.commit('setList', res.result)
        resolve(res.result)
      })
    },
    // 是否选中
    isCelectGoods (ctx, { skuId, flag }) {
      const list = ctx.state.list.map((item) => {
        if (item.skuId === skuId) {
          item.selected = flag
        }
        return item
      })

      ctx.commit('setList', list)
    },
    // 修改数量
    upDateNumGoods (ctx, { skuId, num }) {
      const list = ctx.state.list.map((item) => {
        if (item.skuId === skuId) {
          item.count = num
        }
        return item
      })

      ctx.commit('setList', list)
    },
    // 是否全选
    isAllcelected (ctx, flag) {
      const list = ctx.state.list.map((item) => {
        item.selected = flag
        return item
      })

      ctx.commit('setList', list)
    }
  },
  getters: {
    // 计算有效商品列表 isEffective = true  filter
    effectiveList (state) {
      return state.list.filter(item => item.isEffective === true)
    },
    // 有效商品总数量 把effctiveList中的每一项的count叠加起来
    allCount (state, getters) {
      // const list = state.list.filter(item => item.isEffective === true)
      return getters.effectiveList.reduce((a, c) => a + c.count, 0)
    },
    // 总钱数  = 所有单项的钱数累加  单项的钱数 = 数量 * 单价
    allPrice (state, getters) {
      // const list = state.list.filter(item => item.isEffective === true)
      return getters.effectiveList.reduce((a, c) => a + c.count * c.nowPrice, 0)
    },
    // 已选中列表 selected = true
    selectedList (state) {
      return state.list.filter(item => item.selected)
    },
    // 已选择列表总钱数  selectedList 中所有项的单价*数据进行叠加
    allSelectedPrice (state, getters) {
      return getters.selectedList.reduce((a, c) => a + c.count * c.nowPrice, 0)
    },
    // 已选择列表总数  selectedList
    allSelectedtotal (state, getters) {
      return getters.selectedList.reduce((a, c) => a + c.count, 0)
    },
    // 全选：有效列表中的seleted字段全为true 才为true
    isAll (state, getters) {
      return getters.effectiveList.every(item => item.selected)
    }
  }
}
